package com.szqhyxj.youxiangju.biz.partnerHome.shop.order;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;
import com.szqhyxj.youxiangju.sys.BaseResult;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

class DeliveryOrderPresenter extends AbBasePresenter implements DeliveryOrderContract.Presenter {

    private final DeliveryOrderContract.View view;
    private SubordinatePartnerOrderModel.PartnerOrderOutEntity entity;
    public DeliveryOrderPresenter(DeliveryOrderContract.View view){
        this.view = view;
        entity = new SubordinatePartnerOrderModel.PartnerOrderOutEntity();
    }


    @Override
    public void init(String orderId){
        entity.setOrderNo(orderId);

        addDisposables(SubordinatePartnerOrderModel.getOutPosList(orderId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
        .subscribe(new Consumer<BaseResult<OrderPosListInfo>>(){
            @Override
            public void accept(BaseResult<OrderPosListInfo> orderDeetailsBaseResult) throws Exception{
                view.dismissLoading();
                if(orderDeetailsBaseResult.businessLogicSuccess()){
                    view.showOrderDetails(orderDeetailsBaseResult.getData());
                }else {
                    view.showToast(orderDeetailsBaseResult.getMessage());
                }
            }
        }, new Consumer<Throwable>(){
            @Override
            public void accept(Throwable throwable) throws Exception{
                view.showToast(throwable.getMessage());
                view.dismissLoading();
            }
        }));
    }



    @Override
    public void setDistributionType(int distributionType){
        entity.setDistributionType(distributionType);
    }

    @Override
    public void setTrackingNumber(String trackingNumber){
        entity.setTrackingNumber(trackingNumber);
    }

    @Override
    public void setDevNo(String devNo){
        entity.setDevNo(devNo);
    }

    @Override
    public void confirmOut(List<StockPOListItem> listItems,int number){

        if(entity.getDistributionType() == 2 && (TextUtils.isEmpty(entity.getTrackingNumber())||entity.getTrackingNumber().length()==0)){
            view.showToast("请填写物流单号");
            return;
        }

        int index = 0;
        String[] devNos = new String[number];
        for(int i=0;i<listItems.size();i++){
            StockPOListItem poListItem = listItems.get(i);
            if(poListItem.isChoice()){
                devNos[index] = poListItem.getDevNo();
                index++;
            }
        }
        Gson gson = new Gson();

        setDevNo(gson.toJson(devNos));

        addDisposables(SubordinatePartnerOrderModel.confirmOut(entity)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult>(){
                    @Override
                    public void accept(BaseResult baseResult) throws Exception{
                        view.dismissLoading();
                        if(baseResult.businessLogicSuccess()){
                            view.navSucceed();
                        }else {
                            view.showToast(baseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.dismissLoading();
                        view.showToast(throwable.getMessage());
                    }
                }));
    }

    @Override
    public void addDisposables(Disposable disposable){
        super.addDisposables(disposable);
        view.showLoading();
    }
}